package com.hc.blogssm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hc.blogssm.services.BlogServices;

@Controller
@RequestMapping(value="/blog")
public class BlogController {
	
	@Autowired
	private BlogServices blogServices;
	
	@RequestMapping(value="/findAllContent")
	public String findAllContent(Map<String,Object> pageMap,@RequestParam int pageIndex,@RequestParam int pageSize,@RequestParam String usersId,HttpSession session) {
		
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("usersId", usersId);
		map.put("pageIndex", pageSize*(pageIndex-1));
		
		map.put("pageSize", pageSize);
		
		pageMap.put("pageIndex",pageIndex);
		pageMap.put("pageSize",pageSize);
		
		
		Map<String,Object> countMap = blogServices.findCount(map);
		pageMap.put("countMap",countMap);
		long page=(Long)countMap.get("count");
		pageMap.put("totalPage", page%pageSize==0?(page/pageSize):(page/pageSize+1));
		
		List<Map<String,Object>> contentList= blogServices.findAllContent(map);
		session.setAttribute("contentList", contentList);
		
		
		return "index";
	}
	
	@RequestMapping(value="/addMsg")
	public String addMsg(Map<String,Object> msgMap,@RequestParam String usersId,@RequestParam String addcountent){
		
		Map<String,Object> map= new HashMap<String, Object>();
		
		map.put("usersId", usersId);
		map.put("addcountent", addcountent);
		
		int addMap=blogServices.addMsg(map);
		
		if(addMap==0){
			msgMap.put("errMsg", "提交失败！内容不能为空");
			return "addmsg";
		}else {
			msgMap.put("addMap",addMap);
			return "redirect:/blog/findAllContent?usersId="+usersId+" &pageIndex=1&pageSize=2";
		}
	}
	@RequestMapping(value="/deleteMsgById")
	public String deleteMsgById(Map<String,Object> msgMap,@RequestParam String contentId,@RequestParam String usersId){
		
		Map<String,Object> map= new HashMap<String, Object>();
		
		map.put("contentId", contentId);
		
		int deleteMap = blogServices.deleteMsgById(map);
		
		return "redirect:/blog/findAllContent?usersId="+usersId+" &pageIndex=1&pageSize=2";
	}
	
}
